package com.cn.aam.checaiduo.ui.more;

import android.Manifest;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.BuildConfig;
import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListener;
import com.cn.aam.checaiduo.backend.observer.OnRequestV2Listener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.AppConfigB;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.bean.base.BaseResponseB;
import com.cn.aam.checaiduo.bean.base.NormalResponseB;
import com.cn.aam.checaiduo.center.CustomDialogCenter;
import com.cn.aam.checaiduo.ui.arouter.UIRouter;
import com.cn.aam.checaiduo.ui.arouter.section.RouterMore;
import com.cn.aam.checaiduo.utils.CacheUtils;
import com.cn.aam.checaiduo.utils.MyViewsUtils;
import com.cn.aam.checaiduo.widget.WMSnackBar;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;


@Route(path = UIRouter.MORE.SETTING, name = "系统设置")
public class SettingActivity extends BaseActivity {

    @BindView(R.id.cb_notification)
    CheckBox cbNotification;
    @BindView(R.id.tv_version)
    TextView tvVersion;

    private boolean isClose = true;//是否关闭短信提醒


    @Override
    protected int layoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void onGenerate() {
        RetrofitHome.getInstance()
                .getInternalApi()
                .getAppConfig(AppData.getToken())
                .compose(RxSchedulers.<NormalResponseB<AppConfigB>>toMain())
                .subscribe(new OnRequestV2Listener<AppConfigB>(this) {
                    @Override
                    public void onNext(NormalResponseB<AppConfigB> value) {
                        super.onNext(value);
                        if (value.getIsLogged() != Const.STATE_LOGIN) {
                            goLogin();
                            return;
                        }
                        if (value.getCode() == Const.REQUEST_FAILED) {
                            return;
                        }
                        String isSubcribe = value.getData().getSms_subscribe();
                        isClose = isSubcribe.equals("0");
                        cbNotification.setChecked(isClose);

                    }
                });

    }

    @Override
    protected void initView() {
        //toolbar
        ToolBarManager.with(this, WMContent)
                .setTitle("设置")
                .setNavigationIcon(R.drawable.ic_nav_arrow_white);
        //version
        tvVersion.setText("V" + BuildConfig.VERSION_NAME);


        cbNotification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doUpdateSMS(cbNotification.isChecked());
            }
        });
    }

    //是否关闭短信提醒
    private void doUpdateSMS(final boolean isClose) {
        String sms = isClose ? "0" : "1";
        RetrofitHome.getInstance().getInternalApi()
                .updateSMS(AppData.getToken(), sms)
                .compose(RxSchedulers.<BaseResponseB>toMain())
                .subscribe(new OnRequestListener<BaseResponseB>(this) {
                    @Override
                    public void onNext(BaseResponseB value) {
                        super.onNext(value);
                        if (value.getCode() == Const.REQUEST_FAILED) {
                            cbNotification.setChecked(SettingActivity.this.isClose);
                            return;
                        }
                        SettingActivity.this.isClose = isClose;
                        WMSnackBar.show(WMContent, "操作成功");
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        cbNotification.setChecked(SettingActivity.this.isClose);
                    }
                });
    }

    /**
     * @description 修改密码
     */
    @OnClick(R.id.block_change_pwd)
    public void onClickChangePwd() {
        if (MyViewsUtils.isFastDoubleClick()) return;
        UIRouter.getInstance().openPage(mContext, UIRouter.MORE.CHANGE_MOBILE_PWD);
    }

    /**
     * @description 清理缓存
     */
    @OnClick(R.id.block_clear_cache)
    public void onClickClearCache() {
        if (MyViewsUtils.isFastDoubleClick()) return;
        new RxPermissions(this)
                .request(Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean granted) throws Exception {
                        if (granted)
                            showCacheConfirmDialog();
                        else
                            showPermissionDialog();
                    }
                });
    }

    private void showCacheConfirmDialog() {
        CustomDialogCenter.getInstance()
                .showTipsDialog(mContext, "是否清理缓存", "否", "是", new CustomDialogCenter.OnTipClick() {
                    @Override
                    public void onLeftButtonClick() {

                    }

                    @Override
                    public void onRightButtonClick() {
                        CacheUtils.clearAllCache(mContext);
                        WMSnackBar.show(WMContent, "清除缓存成功");
                    }
                });
    }

    private void showPermissionDialog() {
        CustomDialogCenter.getInstance().showPermissionTipDialog(mContext, "没有相关权限，APP将无法正常删除缓存");
    }
}
